package org.bayareadiplomacy.tournamatic.model


object RandomAllocator {

	def allocate(round: Round): List[Board] = {  
		val shuffledPlayers = round.attending.toArray
	  
		Shuffler.functionalShuffle(shuffledPlayers)
	 
	    allocatePlayersToBoard(round, List.fromArray(shuffledPlayers), round.variant, 1)
	}
 
	def allocatePlayersToBoard(round: Round, people: List[Person], variant: Variant, boardNumber: Int) : List[Board] =  {
		
		val (assignable, remaining) = people splitAt variant.numberOfPowers
	   
		val shuffledVariants = variant.powers.toArray
		
		Shuffler.functionalShuffle(shuffledVariants)
  
		val powersToPlayers = Map[Power,Person]((List.fromArray(shuffledVariants) zip assignable): _*)

		val theBoard = new Board(round, boardNumber.toString, powersToPlayers);
  
		val nextBoardNumber = boardNumber + 1
  
		if (remaining.isEmpty) theBoard :: Nil
		else theBoard :: allocatePlayersToBoard(round, remaining, variant, nextBoardNumber)
	}
  
	implicit def listToJavaList[T](l: List[T]) = java.util.Arrays.asList(l.toArray: _*)
}

